//
//  CollectViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/4/3.
//

#import "CollectViewController.h"
#import "CollectTableViewCell.h"
#import "MapDetailViewController.h"
#import "MapDeletedPopView.h"
@interface CollectViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTintColor = UIColor.whiteColor;
    self.zx_navTitle = SQNSLocalString(@"我的收藏");
}

- (void)SQ_UIViewForSelfView{
    UIImageView * bg = [[UIImageView alloc]init];
    bg.image = SQGetImage(@"me背景");
    bg.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.tableView.backgroundColor = UIColor.clearColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view).offset(TopHeight);
            make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-50);
    }];
    [self SQ_tableViewAddRefresh];
    [self SQ_Header];
}
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [HttpTool post:Url15 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page = 1;
        [self.tableView.mj_header endRefreshing];
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [PointModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_header endRefreshing];
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (void)SQ_Foother{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [HttpTool post:Url15 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page ++;
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [PointModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CollectTableViewCell"];
    if (!cell) {
        cell = [[CollectTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"CollectTableViewCell"];
    }
    PointModel* model = [self.jsonArray objectAtIndex:indexPath.item];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    WS(weakSelf);
    [cell.collectBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_righBtnDeleted:model];
    }];
    return cell;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  [CollectTableViewCell FSCellHeightForTableView:tableView indexPath:indexPath cellContentViewWidth:0 bottomOffset:25];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PointModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    MapDetailViewController * vc = [[MapDetailViewController alloc]init];
    vc.model = [PointModel new];
    vc.model.userid = [NSString stringWithFormat:@"%@",model.pid];
    [self SQ_pushViewControllerFull:vc];
   
}
-(void)SQ_righBtnDeleted:(PointModel*)model{
    MapDeletedPopView * vc = [[MapDeletedPopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 200+FootHeight)];
    vc.la1.text = SQNSLocalString(@"是否取消收藏?");
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                             parentView:[Manager SQ_getCurrentViewController].view
                                               popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
      pop.hemStyle = LSTHemStyleBottom;
      [pop pop];
      LSTPopViewWK(pop)
      [pop setBgClickBlock:^{
          [wk_pop dismiss];
      }];
    [vc.leftBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
            [wk_pop dismiss];
    }];
    WS(weakSelf);
    [vc.rightBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
        [dic setValue:model.pid forKey:@"pid"];
         [dic setValue:@"0" forKey:@"type"];
        [HttpTool post:Url14 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
            [wk_pop dismiss];
            [weakSelf.jsonArray removeObject:model];
            [weakSelf.tableView reloadData];
        } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
            [YJProgressHUD showMsgWithoutView:httpModel.message];
        }];
    }];
}

@end
